package com.aixuexi.item.service.impl;

import com.aixuexi.item.entity.Item;
import com.aixuexi.item.dao.ItemDao;
import com.aixuexi.item.service.ItemService;
import com.aixuexi.item.vo.ItemVo;
import com.aixuexi.item.vo.PageVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * (Item)表服务实现类
 *
 * @author makejava
 * @since 2022-02-15 09:33:21
 */
@Service("itemService")
public class ItemServiceImpl implements ItemService {
    @Resource
    private ItemDao itemDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Item queryById(Integer id) {
        return this.itemDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param item        筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Item> queryByPage(Item item, PageRequest pageRequest) {
        long total = this.itemDao.count(item);
        return new PageImpl<>(this.itemDao.queryAllByLimit(item, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param item 实例对象
     * @return 实例对象
     */
    @Override
    public Item insert(Item item) {
        this.itemDao.insert(item);
        return item;
    }

    /**
     * 修改数据
     *
     * @param item 实例对象
     * @return 实例对象
     */
    @Override
    public Item update(Item item) {
        this.itemDao.update(item);
        return this.queryById(item.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.itemDao.deleteById(id) > 0;
    }


    @Override
    public PageVo<ItemVo> getItems(String categoryIds, String itemName, int currentPage, int pageSize) {
        Set<Integer> ids = new HashSet<>();

        if (StringUtils.isNotBlank(categoryIds)) {
            String[] split = categoryIds.split(",");
            for (int i = 0; i < split.length; i++) {
                Set<Integer> integers = itemDao.countBySelect(Integer.parseInt(split[i]), itemName);
                if (integers == null || integers.size() == 0){
                    ids = null;
                    break;
                }else {
                    if (i == 0){
                        ids = integers;
                    }else {
                        ids.retainAll(integers);
                    }
                }

            }
        } else {
            ids = itemDao.countBySelect(null, itemName);
        }

        PageVo<ItemVo> pageVo = new PageVo<>();
        pageVo.setCurrPage(currentPage);
        pageVo.setPageSize(pageSize);
        if (CollectionUtils.isEmpty(ids)){
            pageVo.setTotal(0);
        }else {
            int start = (currentPage - 1) * pageSize;
            int end = start + pageSize;
            List<ItemVo> itemVos = itemDao.selectByIdsLimit(ids,start,end);
            pageVo.setTotal(ids.size());
            pageVo.setData(itemVos);
        }
        return pageVo;


    }



}
